home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 366_01 / ue311c1.arc / AMIGADOS.C next >
C/C++ Source or Header  |  1990-08-16  |  22KB  |  892 lines

  1. /*    AMIGADOS.C:    Operating specific I/O and Spawning functions
  2.             for MicroEMACS 3.10
  3.             (C)Copyright 1988 by Daniel M. Lawrence
  4. */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #if    AMIGA
  9. #include    <exec/types.h>
  10. #include    <exec/io.h>
  11. #include    <intuition/intuition.h>
  12. #include    <devices/console.h>
  13. #include    "eproto.h"
  14. #include        "edef.h"
  15. #include    "elang.h"
  16.  
  17. #define INTUITION_REV    0L
  18. #define    NEW         1006L
  19. #define    CRWIDTH        8
  20. #define    CRHEIGHT    8
  21.  
  22. struct IntuitionBase *IntuitionBase;
  23. struct Window *win;
  24. struct IOStdReq con;        /* ptr to console device driver handle */
  25.  
  26. /*    Intuition Function type declarations    */
  27.  
  28. struct IntuitionBase *OpenLibrary();
  29. struct Window *OpenWindow();
  30. struct IntuiMessage *GetMsg();
  31.  
  32. typedef struct {
  33.     short rw_code;        /* normal keycode to generate */
  34.     short rw_scode;        /* shifted  "  */
  35.     short rw_ccode;        /* control  "  */
  36. } RKEY;
  37.  
  38. /* raw keycode scan code to emacs keycode translation table */
  39.  
  40. RKEY keytrans[0x60] = {
  41.  
  42. /*    CODE    NORM    SHIFT    CTRL */
  43. /*    0x00,*/    '`',    '~',    0,
  44. /*    0x01,*/    '1',    '!',    0,
  45. /*    0x02,*/    '2',    '@',    0,
  46. /*    0x03,*/    '3',    '#',    0,
  47. /*    0x04,*/    '4',    '$',    0,
  48. /*    0x05,*/    '5',    '%',    0,
  49. /*    0x06,*/    '6',    '^',    0,
  50. /*    0x07,*/    '7',    '&',    0,
  51. /*    0x08,*/    '8',    '*',    0,    
  52. /*    0x09,*/    '9',    '(',    0,
  53. /*    0x0a,*/    '0',    ')',    0,
  54. /*    0x0b,*/    '-',    '_',    0,
  55. /*    0x0c,*/    '=',    '+',    0,
  56. /*    0x0d,*/    '\\',    '|',    0,
  57. /*    0x0e,*/    0,    0,    0,
  58. /*    0x0f,*/    0,    0,    0,
  59. /*    0x10,*/    'q',    'Q',    CTRL|'Q',
  60. /*    0x11,*/    'w',    'W',    CTRL|'W',
  61. /*    0x12,*/    'e',    'E',    CTRL|'E',
  62. /*    0x13,*/    'r',    'R',    CTRL|'R',
  63. /*    0x14,*/    't',    'T',    CTRL|'T',
  64. /*    0x15,*/    'y',    'Y',    CTRL|'Y',
  65. /*    0x16,*/    'u',    'U',    CTRL|'U',
  66. /*    0x17,*/    'i',    'I',    CTRL|'I',
  67. /*    0x18,*/    'o',    'O',    CTRL|'O',
  68. /*    0x19,*/    'p',    'P',    CTRL|'P',
  69. /*    0x1a,*/    '[',    '{',    0,
  70. /*    0x1b,*/    ']',    '}',    0,
  71. /*    0x1c,*/    0,    0,    0,
  72. /*    0x1d,*/    '1',    SPEC|'>',    0,
  73. /*    0x1e,*/    '2',    SPEC|'N',    0,
  74. /*    0x1f,*/    '3',    SPEC|'V',    0,
  75. /*    0x20,*/    'a',    'A',    CTRL|'A',
  76. /*    0x21,*/    's',    'S',    CTRL|'S',
  77. /*    0x22,*/    'd',    'D',    CTRL|'D',
  78. /*    0x23,*/    'f',    'F',    CTRL|'F',
  79. /*    0x24,*/    'g',    'G',    CTRL|'G',/*    0x25,*/    'h',    'H',    CTRL|'H',
  80. /*    0x26,*/    'j',    'J',    CTRL|'J',
  81. /*    0x27,*/    'k',    'K',    CTRL|'K',
  82. /*    0x28,*/    'l',    'L',    CTRL|'L',
  83. /*    0x29,*/    ';',    ':',    0,
  84. /*    0x2a,*/    39,    34,    0,
  85. /*    0x2b,*/    0,    0,    0,
  86. /*    0x2c,*/    0,    0,    0,
  87. /*    0x2d,*/    '4',    SPEC|'B',    0,
  88. /*    0x2e,*/    '5',    0,        0,
  89. /*    0x2f,*/    '6',    SPEC|'F',    0,
  90.     /* this key is probably mapped on forign AIMIGA keyboards */
  91. /*    0x30,*/    0,    0,    0,
  92. /*    0x31,*/    'z',    'Z',    CTRL|'Z',
  93. /*    0x32,*/    'x',    'X',    CTRL|'X',
  94. /*    0x33,*/    'c',    'C',    CTRL|'C',
  95. /*    0x34,*/    'v',    'V',    CTRL|'V',
  96. /*    0x35,*/    'b',    'B',    CTRL|'B',
  97. /*    0x36,*/    'n',    'N',    CTRL|'N',
  98. /*    0x37,*/    'm',    'M',    CTRL|'M',
  99. /*    0x38,*/    ',',    '<',    0,
  100. /*    0x39,*/    '.',    '>',    0,
  101. /*    0x3a,*/    '/',    '?',    0,
  102. /*    0x3b,*/    0,    0,    0,
  103. /*    0x3c,*/    '.',    SPEC|'D',    0,
  104. /*    0x3d,*/    '7',    SPEC|'<',    0,
  105. /*    0x3e,*/    '8',    SPEC|'P',    0,
  106. /*    0x3f,*/    '9',    SPEC|'Z',    0,
  107. /*    0x40,*/    ' ',    SHFT|' ',    0,
  108. /*    0x41,*/    CTRL|'H',    SHFT|'D',    0,
  109. /*    0x42,*/    CTRL|'I',    SHFT|'I',    0,
  110. /*    0x43,*/    CTRL|'M', CTRL|'M', CTRL|'M',
  111. /*    0x44,*/    CTRL|'M', CTRL|'M', CTRL|'M',
  112. /*    0x45,*/    CTRL|'[',    0,    0,
  113. /*    0x46,*/    SPEC|'D',    0,    0,
  114. /*    0x47,*/    0,    0,    0,
  115. /*    0x48,*/    0,    0,    0,
  116. /*    0x49,*/    0,    0,    0,
  117. /*    0x4a,*/    '-',    0,    0,
  118. /*    0x4b,*/    0,    0,    0,
  119. /*    0x4c,*/    SPEC|'P',    SHFT|SPEC|'P',    CTRL|SPEC|'P',
  120. /*    0x4d,*/    SPEC|'N',    SHFT|SPEC|'N',    CTRL|SPEC|'N',
  121. /*    0x4e,*/    SPEC|'F',    SHFT|SPEC|'F',    CTRL|SPEC|'F',
  122. /*    0x4f,*/    SPEC|'B',    SHFT|SPEC|'B',    CTRL|SPEC|'B',
  123. /*    0x50,*/    SPEC|'1',    SHFT|SPEC|'1',    CTRL|SPEC|'1',
  124. /*    0x51,*/    SPEC|'2',    SHFT|SPEC|'2',    CTRL|SPEC|'2',
  125. /*    0x52,*/    SPEC|'3',    SHFT|SPEC|'3',    CTRL|SPEC|'3',
  126. /*    0x53,*/    SPEC|'4',    SHFT|SPEC|'4',    CTRL|SPEC|'4',
  127. /*    0x54,*/    SPEC|'5',    SHFT|SPEC|'5',    CTRL|SPEC|'5',
  128. /*    0x55,*/    SPEC|'6',    SHFT|SPEC|'6',    CTRL|SPEC|'6',
  129. /*    0x56,*/    SPEC|'7',    SHFT|SPEC|'7',    CTRL|SPEC|'7',
  130. /*    0x57,*/    SPEC|'8',    SHFT|SPEC|'8',    CTRL|SPEC|'8',
  131. /*    0x58,*/    SPEC|'9',    SHFT|SPEC|'9',    CTRL|SPEC|'9',
  132. /*    0x59,*/    SPEC|'0',    SHFT|SPEC|'0',    CTRL|SPEC|'0',
  133. /*    0x5a,*/    '(',    0,    0,
  134. /*    0x5b,*/    ')',    0,    0,
  135. /*    0x5c,*/    '/',    0,    0,
  136. /*    0x5d,*/    '*',    0,    0,
  137. /*    0x5e,*/    0,    0,    0,
  138. /*    0x5f,*/    SPEC|'?',    0,    0,
  139. };
  140.  
  141. /* some keyboard keys current states */
  142.  
  143. int r_shiftflag;    /* right shift key */
  144. int l_shiftflag;    /* left shift key */
  145. int r_altflag;        /* right alt key */
  146. int l_altflag;        /* left alt key */
  147. int r_amiflag;        /* right amiga key */
  148. int l_amiflag;        /* left amiga key */
  149. int ctrlflag;        /* control key */
  150. int lockflag;        /* shift lock key */
  151.  
  152. /*    output buffers and pointers    */
  153.  
  154. #define OBUFSIZE    1024L
  155. #define    IBUFSIZE    64    /* this must be a power of 2 */
  156.  
  157. char out_buf[OBUFSIZE+1];    /* output character buffer */
  158. int out_ptr = 0;        /* index to next char to put in buffer */
  159.  
  160. /*    input buffers and pointers    */
  161.  
  162. #define    IBUFSIZE    64    /* this must be a power of 2 */
  163.  
  164. unsigned char in_buf[IBUFSIZE];    /* input character buffer */
  165. int in_next = 0;        /* pos to retrieve next input character */
  166. int in_last = 0;        /* pos to place most recent input character */
  167.  
  168. in_init()    /* initialize the input buffer */
  169.  
  170. {
  171.     in_next = in_last = 0;
  172. }
  173.  
  174. in_check()    /* is the input buffer non-empty? */
  175.  
  176. {
  177.     if (in_next == in_last)
  178.         return(FALSE);
  179.     else
  180.         return(TRUE);
  181. }
  182.  
  183. in_put(event)
  184.  
  185. int event;    /* event to enter into the input buffer */
  186.  
  187. {
  188.     in_buf[in_last++] = event;
  189.     in_last &= (IBUFSIZE - 1);
  190. }
  191.  
  192. int in_get()    /* get an event from the input buffer */
  193.  
  194. {
  195.     register int event;    /* event to return */
  196.  
  197.     event = in_buf[in_next++];
  198.     in_next &= (IBUFSIZE - 1);
  199.     return(event);
  200. }
  201.  
  202. /*
  203.  * This function is called once to set up the terminal device streams.
  204.  * On VMS, it translates TT until it finds the terminal, then assigns
  205.  * a channel to it and sets it raw. On CPM it is a no-op.
  206.  */
  207. ttopen()
  208. {
  209.     struct NewWindow new_win;
  210.     int i;
  211. #if    AZTEC
  212.     extern    Enable_Abort;    /* Turn off ctrl-C interrupt */
  213.  
  214.     Enable_Abort = 0;    /* for the Manx compiler */
  215. #endif
  216.  
  217.     /* open the intuition library */
  218.     IntuitionBase = (struct IntuitionBase *)
  219.         OpenLibrary("intuition.library", INTUITION_REV);
  220.     if (IntuitionBase == NULL) {
  221.         printf("%%Can not open Intuition\n");
  222.         exit(-1);
  223.     }
  224.  
  225.     /* initialize the new windows attributes */
  226.     new_win.LeftEdge = 0;
  227.     new_win.TopEdge = 0;
  228.     new_win.Width = 640;
  229.     new_win.Height = 200;
  230.     new_win.DetailPen = 0;
  231.     new_win.BlockPen = 1;
  232.     new_win.Title = (unsigned char *)"MicroEMACS 3.10g/Amiga";
  233.     new_win.Flags = WINDOWCLOSE | SMART_REFRESH | ACTIVATE |
  234.         WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING | SIZEBRIGHT |
  235.         RMBTRAP | NOCAREREFRESH;
  236.     new_win.IDCMPFlags = CLOSEWINDOW | NEWSIZE | MOUSEBUTTONS |
  237.         RAWKEY;
  238.     new_win.Type = WBENCHSCREEN;
  239.     new_win.FirstGadget = NULL;
  240.     new_win.CheckMark = NULL;
  241.     new_win.Screen = NULL;
  242.     new_win.BitMap = NULL;
  243.     new_win.MinWidth = 100;
  244.     new_win.MinHeight = 25;
  245.     new_win.MaxWidth = 640;
  246.     new_win.MaxHeight = 200;
  247.  
  248.     /* open the window! */
  249.     win = (struct Window *)OpenWindow(&new_win);
  250.     if (win == NULL) {
  251.         printf("%%Can not open a window\n");
  252.         exit(-2);
  253.     }
  254.  
  255.     /* and open up the console for output */
  256.     con.io_Data = (APTR)win;
  257.     OpenDevice("console.device", 0, &con, 0);
  258.  
  259.     /* and init all the keyboard flags */
  260.     r_shiftflag = FALSE;
  261.     l_shiftflag = FALSE;    r_altflag = FALSE;
  262.     l_altflag = FALSE;
  263.     r_amiflag = FALSE;
  264.     l_amiflag = FALSE;
  265.     ctrlflag = FALSE;
  266.     lockflag = FALSE;
  267.  
  268.     /* initialize our private event queue */
  269.     in_init();
  270.  
  271.     /* set the current sizes */
  272.     newwidth(TRUE, 77);
  273.     newsize(TRUE, 23);
  274.  
  275.     /* on all screens we are not sure of the initial position
  276.        of the cursor                    */
  277.     ttrow = 999;
  278.     ttcol = 999;
  279. }
  280.  
  281. /*
  282.  * This function gets called just before we go back home to the command
  283.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  284.  * Another no-operation on CPM.
  285.  */
  286. ttclose()
  287.  
  288. {
  289.     /* make sure there is no pending output */
  290.     ttflush();
  291.  
  292.     /* and now close up shop */
  293.     CloseDevice(&con);
  294.     CloseWindow(win);
  295.     OpenWorkBench();
  296. }
  297.  
  298. /*
  299.  * Write a character to the display. On VMS, terminal output is buffered, and
  300.  * we just put the characters in the big array, after checking for overflow.
  301.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  302.  * MS-DOS (use the very very raw console output ro